//
//  SigningListController.m
//  LuCaiZhu
//
//  Created by Stella on 2018/9/21.
//
#import "Header.h"
#import "UIColor+NN.h"
#import "SignListCell.h"
#import "CKPuslichTool.h"
#import "MJRefresh.h"
#import "UserModel.h"
#import "HandleNetProduct.h"
#import "SignListMode.h"
#import "SigningProcessController.h"
#import "YJSegmentedControl.h"
#import "SigningListController.h"

@interface SigningListController (){
    int count;
}
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (assign ,nonatomic) NSInteger productsType;
@property (weak, nonatomic) YJSegmentedControl * segment;
@property (weak, nonatomic) UILabel *finishNum;
@property (strong, nonatomic)NSMutableArray *datas;
@property (weak, nonatomic) IBOutlet UIView *noDataView;


@property (weak, nonatomic) IBOutlet UIButton *leftBt;
@property (weak, nonatomic) IBOutlet UIButton *rightBt;
@property (weak, nonatomic) IBOutlet UILabel *flageView;


@end

@implementation SigningListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电子签约";
    _datas = [[NSMutableArray alloc] init];
    self.mTableView.estimatedRowHeight=180;
    self.mTableView.rowHeight = UITableViewAutomaticDimension;
    self.productsType = 0;

    count =1;
    _mTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count=1;
        [self getlistDataArray];
    }];
    _mTableView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        count++;
        [self getlistDataArray];
    }];
    
    [self changeTypes:_leftBt];
    
}

- (IBAction)changeTypes:(UIButton *)sender {
    self.leftBt.selected = NO;
    self.rightBt.selected = NO;
    sender.selected=!sender.selected;
    self.productsType = sender.tag;
    count = 1;
    [self getlistDataArray];
    CGPoint centerPoint=self.flageView.center;
    centerPoint.x= sender.center.x;
    self.flageView.center=centerPoint;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getlistDataArray];
}

- (void)getListNumbers{
    UserModel *mode=[UserModel getUser];
    int mid=[mode.CustomerID intValue];
    [HandleNetProduct getSigningNumWithCustomerId:mid CurrencyType:_currency DataSuccess:^(NSDictionary *data) {
        NSString *unfinishedNum ;
        if ([data[@"NoFinishNum"] intValue]==0) {
            unfinishedNum = @"签约中";
        }else{
            unfinishedNum = [NSString stringWithFormat:@"签约中(%@)",data[@"NoFinishNum"]];
        }
        [_leftBt setTitle:unfinishedNum forState:UIControlStateNormal];
        [_leftBt setTitle:unfinishedNum forState:UIControlStateSelected];
        
        NSString *finishedNum ;
        if ([data[@"FinishNum"] intValue]==0) {
            finishedNum = @"签约完成";
        }else{
            finishedNum = [NSString stringWithFormat:@"签约完成(%@)",data[@"FinishNum"]];
        }
        [_rightBt setTitle:finishedNum forState:UIControlStateNormal];
        [_rightBt setTitle:finishedNum forState:UIControlStateSelected];
    } Failure:^(NSString *errorInfo) {
        
    }];
}

-(void)getlistDataArray{
    [self getListNumbers];
    UserModel *mode=[UserModel getUser];
    int mid=[mode.CustomerID intValue];
    NSString *type ;
    if (self.productsType == 0) {
        type = @"A";
    }else{
        type = @"B";
    }
    NSString *page = [NSString stringWithFormat:@"%d",count];
    [HandleNetProduct getSigningListforCustomerID:mid Status:type Currency:_currency Page:page Success:^(NSArray *data) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
        if (count==1) {
            [self.datas removeAllObjects];
        }
        if (data.count>0) {
            [_datas addObjectsFromArray:data];
        }
        if (self.datas.count==0) {
            self.noDataView.hidden=NO;
            
        }else{
            self.noDataView.hidden=YES;
        }
        [self.mTableView reloadData];
    } Failure:^(NSString *errorInfo) {
        [self.mTableView.mj_header endRefreshing];
        [self.mTableView.mj_footer endRefreshing];
    }];
}

- (void)goProcessController:(UIButton *)sender{
    NSDictionary *dic = _datas[sender.tag];
    SignListMode *model = [SignListMode mj_objectWithKeyValues:dic];
    SigningProcessController *vc = (SigningProcessController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SigningProcessController"];
    vc.bookingId = model.BookingId;
    vc.returnBlock = ^{
        count=1;
        [self getlistDataArray];
    };
    
    NSString *type ;
    if (self.productsType == 0) {
        type = @"A";
    }else{
        type = @"B";
    }
    vc.SigningStatus = type;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - Table view data source
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewAutomaticDimension;
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SignListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SignListCell"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.productsType == 0) {
        //签约中
        cell.statusLa.textColor = DEF_STRING_COLOR(@"E7AA6E");
    }else{
        //已完成
        cell.statusLa.textColor = MAIN_COLOR_Red;
    }
    NSDictionary *dic = _datas[indexPath.section];
    SignListMode *model = [SignListMode mj_objectWithKeyValues:dic];
    cell.signMode = model;
    cell.seeButton.tag = indexPath.section;
    [cell.seeButton addTarget:self action:@selector(goProcessController:) forControlEvents:UIControlEventTouchDown];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01f;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    
}


@end
